package com.group7.mall.service.impl;

import com.group7.mall.convert.AuditConvert;
import com.group7.mall.dao.TbMessageAuditFormDao;
import com.group7.mall.dao.TbSellerDao;
import com.group7.mall.dao.TbStoreAuditFormDao;
import com.group7.mall.dao.TbUserDao;
import com.group7.mall.dto.StoreAuditFormPreviewDto;
import com.group7.mall.entity.TbMessageAuditForm;
import com.group7.mall.entity.TbSeller;
import com.group7.mall.entity.TbStoreAuditForm;
import com.group7.mall.entity.TbUser;
import com.group7.mall.req.StoreAuditPageRequest;
import com.group7.mall.resp.StoreAuditPageResponse;
import com.group7.mall.service.AdminService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    private TbStoreAuditFormDao tbStoreAuditFormDao;

    @Resource
    private TbUserDao tbUserDao;

    @Resource
    private TbSellerDao tbSellerDao;

    @Resource
    private TbMessageAuditFormDao tbMessageAuditFormDao;

    @Override
    public StoreAuditPageResponse<StoreAuditFormPreviewDto> showAuditByPage(StoreAuditPageRequest storeAuditPageRequest) {
        storeAuditPageRequest.setPageStart((storeAuditPageRequest.getPageNo() - 1) * storeAuditPageRequest.getPageSize());
        List<TbStoreAuditForm> tbAuditFormList = tbStoreAuditFormDao.queryPageByLimit(storeAuditPageRequest);
        List<StoreAuditFormPreviewDto> storeAuditFormPreviewDtoList = AuditConvert.INSTANCE.convertTbStoreAuditFormListToStoreAuditFormPreviewDtoList(tbAuditFormList);
        storeAuditFormPreviewDtoList.forEach(storeAuditFormPreviewDto -> {
            TbUser tbUser = tbUserDao.queryByUserOpenId(storeAuditFormPreviewDto.getUserOpenId());
            storeAuditFormPreviewDto.setUserName(tbUser.getUsername());
        });
        StoreAuditPageResponse<StoreAuditFormPreviewDto> storeAuditPageResponse = new StoreAuditPageResponse<>();
        storeAuditPageResponse.setPageSize(storeAuditPageRequest.getPageSize());
        storeAuditPageResponse.setCurrent(storeAuditPageRequest.getPageNo());
        storeAuditPageResponse.setRecords(storeAuditFormPreviewDtoList);
        TbStoreAuditForm tbStoreAuditForm = new TbStoreAuditForm();
        tbStoreAuditForm.setAuditStatus(storeAuditPageRequest.getAuditStatus());
        storeAuditPageResponse.setTotal(tbStoreAuditFormDao.count(tbStoreAuditForm));
        return storeAuditPageResponse;
    }

    @Override
    public StoreAuditPageResponse<TbSeller> showMessageAuditByPage(StoreAuditPageRequest storeAuditPageRequest) {
        storeAuditPageRequest.setPageStart((storeAuditPageRequest.getPageNo() - 1) * storeAuditPageRequest.getPageSize());
        List<TbMessageAuditForm> tbMessageAuditFormList = tbMessageAuditFormDao.queryPageByLimit(storeAuditPageRequest);
        List<TbSeller> tbSellerList = new ArrayList<>();
        tbMessageAuditFormList.forEach(tbMessageAuditForm -> {
            tbSellerList.add(tbSellerDao.queryById(tbMessageAuditForm.getStoreId()));
        });
        StoreAuditPageResponse<TbSeller> storeAuditPageResponse = new StoreAuditPageResponse<>();
        storeAuditPageResponse.setPageSize(storeAuditPageRequest.getPageSize());
        storeAuditPageResponse.setCurrent(storeAuditPageRequest.getPageNo());
        storeAuditPageResponse.setRecords(tbSellerList);
        TbMessageAuditForm tbMessageAuditForm = new TbMessageAuditForm();
        tbMessageAuditForm.setAuditStatus(0);
        storeAuditPageResponse.setTotal(tbMessageAuditFormDao.count(tbMessageAuditForm));
        return storeAuditPageResponse;
    }

    @Override
    public TbMessageAuditForm showMessageAuditDetail(Integer storeId) {
        return tbMessageAuditFormDao.queryByStoreId(storeId);
    }

    @Override
    public TbStoreAuditForm showStoreAuditFormDetail(TbStoreAuditForm tbStoreAuditForm) {
        return tbStoreAuditFormDao.queryById(tbStoreAuditForm.getAuditId());
    }

    @Override
    public Integer storeAuditingAccess(TbStoreAuditForm tbStoreAuditForm) {
        TbStoreAuditForm tbAuditForm2 = tbStoreAuditFormDao.queryById(tbStoreAuditForm.getAuditId());
        tbAuditForm2.setAuditStatus(1);
        tbAuditForm2.setAuditEndTime(tbStoreAuditForm.getAuditEndTime());
        TbUser tbUser = tbUserDao.queryByUserOpenId(tbAuditForm2.getUserOpenId());
        TbSeller tbSeller = AuditConvert.INSTANCE.convertTbAuditStoreFormToTbSeller(tbAuditForm2);
        tbSeller.setSellerId(tbAuditForm2.getUserOpenId());
        tbSeller.setCreatedTime(tbStoreAuditForm.getAuditEndTime());
        tbSeller.setIsDeleted(0);
        tbSellerDao.insert(tbSeller);
        tbUser.setIdentity(1);
        tbUserDao.update(tbUser);
        return tbStoreAuditFormDao.update(tbAuditForm2);
    }

    @Override
    public Integer storeAuditingNotAccess(TbStoreAuditForm tbStoreAuditForm) {
        return tbStoreAuditFormDao.deleteById(tbStoreAuditForm.getAuditId());
    }
}
